﻿@using XRM.Security.WebUI.Helpers
@model XRM.Security.WebUI.Models.SkziKind.SkziKindModel
@{
    ViewBag.Title = Model.IsEdit ? Model.ShortName : "Добавление вида СКЗИ";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@if (Model.RedirectToUserPage)
{
    @Html.Partial("ReturnLinkPartial", new Tuple<string, string>(Url.Action("Edit", "UserRegister", new { id = Model.UserRegisterId }) + "#divSczi", ""))
}
else
{
    @Html.Partial("ReturnLinkPartial", new Tuple<string, string>(Url.Action("Index"), "Вернуться к списку СКЗИ"))
}


<div>
    @using (Html.BeginForm("Edit", "SkziKind", FormMethod.Post, new{@class="form-horizontal form-horizontal-2"}))
    {
        @Html.AntiForgeryToken()

        const string inputCss = "form-control";

        @Html.HiddenFor(m => m.Id)
        @Html.HiddenFor(m => m.UserRegisterId)
        @Html.HiddenFor(m => m.RedirectToUserPage)

        <div class="form-group">
            @Html.LabelFor(m => m.FullName, new { @class = "control-label required" })
            <div class="col-sm-4">
                @Html.TextBoxFor(m => m.FullName, new { @class = @inputCss })
                @Html.ValidationMessageFor(m => m.FullName)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.ShortName, new { @class = "control-label required" })
            <div class="col-sm-3">
                @Html.TextBoxFor(m => m.ShortName, new { @class = @inputCss })
                @Html.ValidationMessageFor(m => m.ShortName)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.RegistrNumber, new { @class = "control-label required" })
            <div class="col-sm-3">
                @Html.TextBoxFor(m => m.RegistrNumber, new { @class = @inputCss })
                @Html.ValidationMessageFor(m => m.RegistrNumber)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.OperationDocName, new { @class = "control-label" })
            <div class="col-sm-4">
                @Html.TextBoxFor(m => m.OperationDocName, new { @class = @inputCss })
                @Html.ValidationMessageFor(m => m.OperationDocName)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.TechnikalDocName, new { @class = "control-label" })
            <div class="col-sm-4">
                @Html.TextBoxFor(m => m.TechnikalDocName, new { @class = @inputCss })
                @Html.ValidationMessageFor(m => m.TechnikalDocName)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.SertificateFsbList, new { @class = "control-label" })
            <div class="col-sm-3 control-text">
                <div data-bind="template: { name: 'fsb-template', foreach: SertificateFsb }"></div>
                @if (!Model.ReadOnly)
                {
                <a href="#" style="padding-bottom: 20px" data-bind="click: addFsb">Добавить сертификат ФСБ</a>
                }
            </div>
        </div>
        
        <div class="form-group">
            @Html.LabelFor(m => m.SertificateFstkList, new { @class = "control-label" })
            <div class="col-sm-3 control-text">
                <div data-bind="template: { name: 'fstk-template', foreach: SertificateFstk }"></div>
                @if (!Model.ReadOnly)
                {
                    <a href="#" style="padding-bottom: 20px" data-bind="click: addFstk">Добавить сертификат ФСTЭK</a>
                }
            </div>
        </div>
        if(!Model.ReadOnly)
        {
            @Html.Partial("_FormActionsPartial")
        }
    }
</div>

<script type="text/html" id="fsb-template">
    <div class="input-list-item" data-bind="css: { 'input-group': $index() > 0 }">
        <input type="hidden" name="SertificateFsbList[].Id" data-bind="attr: { name: 'SertificateFsbList[' + $index() + '].Id' }, value: id" />
        <input type="text" class="form-control" maxlength="250" name="SertificateFsbList[].Name" data-bind="attr: { name: 'SertificateFsbList[' + $index() + '].Name' }, value: name, hasfocus: isFocused" />
        @if (!Model.ReadOnly)
        {
        <span class="input-group-btn" data-bind="visible: $index() > 0">
            <button title="Удалить сертификат ФСБ" class="btn btn-default" type="button" data-bind="click: $parent.removeFsb">
                <i class="fa fa-times"></i>
            </button>
        </span>
        }
    </div>
</script>
<script type="text/html" id="fstk-template">
    <div class="input-list-item" data-bind="css: { 'input-group': $index() > 0 }">
        <input type="hidden" name="SertificateFstkList[].Id" data-bind="attr: { name: 'SertificateFstkList[' + $index() + '].Id' }, value: id" />
        <input type="text" class="form-control" maxlength="250" name="SertificateFstkList[].Name" data-bind="attr: { name: 'SertificateFstkList[' + $index() + '].Name' }, value: name, hasfocus: isFocused" />
        @if (!Model.ReadOnly)
        {
        <span class="input-group-btn" data-bind="visible: $index() > 0">
            <button title="Удалить сертификат ФСТЭК" class="btn btn-default" type="button" data-bind="click: $parent.removeFstk">
                <i class="fa fa-times"></i>
            </button>
        </span>
        }
    </div>
</script>

@section scripts
{
    <script src="@Url.Content("~/Scripts/knockout-3.2.0.js")"></script>
    <script type="text/javascript">
        function SkziKindViewModel() {
            var self = this;

            self.SertificateFsb = ko.observableArray([
                @{
                    if ((Model.SertificateFsbList != null) && (Model.SertificateFsbList.Count() > 0))
                    {
                        foreach (var item in Model.SertificateFsbList)
                        {
                            <text>{ id: '@item.Id', name: '@Html.Raw(Utils.PrepareStringToJS(item.Name))', isFocused: false },</text>
                        }
                    }
                }
            ]);

            self.addFsb = function () {
                self.SertificateFsb.push({ id: "-1", name: "", isFocused: true });
            };

            self.removeFsb = function () {
                self.SertificateFsb.remove(this);
            };



            self.SertificateFstk = ko.observableArray([
                @{
                    if ((Model.SertificateFstkList != null) && (Model.SertificateFstkList.Count() > 0))
                    {
                        foreach (var item in Model.SertificateFstkList)
                        {
                            <text>{ id: '@item.Id', name: '@Html.Raw(Utils.PrepareStringToJS(item.Name))', isFocused: false },</text>
                        }
                    }
                }
            ]);
            
            self.addFstk = function () {
                self.SertificateFstk.push({ id: "-1", name: "", isFocused: true });
            };

            self.removeFstk = function () {
                self.SertificateFstk.remove(this);
            };
        }

        ko.applyBindings(new SkziKindViewModel());

        $(document).ready(function () {
            @if (Model.ReadOnly)
            {
                <text>ReadOnlyView();</text>
            }
        });
    </script>
}